<?php
// @formatter:off
/**
 * @file pager.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-05-13 04:08:49 +0000 (Mon, 13 May 2013) $
 *
 * $Id: pager.php 64 2013-05-13 04:08:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/themes/default/controlers/generics/pager.php $
 */
// @formatter:on

/**
 * Sets parameters for template /generics/pager.html
 *
 * @param string $link URL to be prepend on each page link.
 * @param integer $pages Amount of pages.
 * @param integer $page Current page (first page is one).
 */
function wcSetPager($link, $pages, $page = 1) {
	if($pages > 1) {
		global $wcThemeAssigns;

		$wcThemeAssigns["PAGER-FIRST"] = "{$link}&page=1";
		$wcThemeAssigns["PAGER-LAST"] = "{$link}&page={$pages}";
		$wcThemeAssigns["PAGER-CURRENT"] = $page;

		$aux = $page - 1;
		$wcThemeAssigns["PAGER-PREVIOUS"] = "{$link}&page=".($aux < 1 ? 1 : $aux);
		$aux = $page + 1;
		$wcThemeAssigns["PAGER-NEXT"] = "{$link}&page=".($aux > $pages ? $pages : $aux);

		$minPage = $page - 5;
		$maxPage = $page + 5;
		$minPage = $minPage >= 1 ? $minPage : 1;
		$maxPage = $maxPage < $pages ? $maxPage : $pages;

		$list = array();
		for($i = $minPage; $i <= $maxPage; $i++) {
			$list[] = array(
				"link" => "{$link}&page={$i}",
				"name" => $i,
				"current" => ($i == $page)
			);
		}
		$wcThemeAssigns["PAGER-PAGES"] = $list;

		if($minPage != 1) {
			$wcThemeAssigns["PAGER-SPACER-PREVIOUS"] = true;
		}
		if($maxPage != $pages) {
			$wcThemeAssigns["PAGER-SPACER-NEXT"] = true;
		}

		$wcThemeAssigns["PAGER-SHOW"] = true;
	}
}
?>